package com.wangsd.web.mqpay.service;

import com.gitee.fastmybatis.core.query.Query;
import com.wangsd.common.exception.BusinessException;
import com.wangsd.web.mqpay.entity.Customer;
import com.wangsd.web.mqpay.entity.CustomerPassageway;
import com.wangsd.web.mqpay.entity.Passageway;
import com.wangsd.web.mqpay.mapper.CustomerPassagewayMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class CustomerPassagewayService {
    @Resource
    private CustomerPassagewayMapper mapper;

    /**
     * 配置客户通道
     * @param customer
     */
    @Transactional
    public int addPassageway(Customer customer) {
        BigDecimal a = new BigDecimal(1);
        if (customer.getWays().size() > 0) {
            mapper.deleteByQuery(new Query().eq("customer_id", customer.getId()));
            List<CustomerPassageway> list = new ArrayList();
            for (Passageway way : customer.getWays()) {
                if (way.getRate().compareTo(a) > -1) {
                    throw new BusinessException("必须输入千分之多少");
                }
                CustomerPassageway customerPassageway = new CustomerPassageway();
                customerPassageway.setCustomerId(customer.getId());
                customerPassageway.setPassagewayId(way.getId());
                customerPassageway.setRate(way.getRate());
                list.add(customerPassageway);
            }
            mapper.saveBatch(list);
        }
        return 1;
    }
}
